<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="zjm" class="ml-2">
          <a-input v-model:value="formState.zjm" class="!w-72" placeholder="药品编号/药品名称/助记码/流水单号" allow-clear />
        </a-form-item>
        <a-form-item name="stdate" label="转移日期" class="ml-3 mr-3">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item label="入库仓库" name="ckbh" class="w-72">
          <SelectWarehouse v-model:value="formState.ckbh" multiple :max-tag-count="1" :max-tag-text-length="5" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3 flex justify-end">
        <BaseExport type="药库" name="库存转移" code="000403" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable :loading="loading" column-code="000403" :data-source="tableData" bordered fill :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            <div>{{ index + 1 }}</div>
          </template>
          <template v-if="column.dataIndex === 'ypmc'">
            <MedicineInfo :value="{ mc: record.ypmc, gg: record.gg, cj: record.cj }" />
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <a-table-summary-cell>合计</a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total.cbje) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>-</a-typography-text>
              </a-table-summary-cell>
              <a-table-summary-cell>
                <a-typography-text>
                  <div class="text-right">{{ formatAmount(total.hjje) }}</div>
                </a-typography-text>
              </a-table-summary-cell>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { formatAmount } from '@/utils/format.js'

const formRef = ref(null)
const total = ref({})
const formState = ref({
  zjm: '',
  operationmode: ['1'],
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, getData, pagination } = useTable({
  url: '/drugStorage/psiform/getInventoryTransferList',
  immediate: true,
  params: formState.value,
  // formatParams: () => {
  //   return {
  //     stdate: formState.value.dateRange?.[0],
  //     eddate: formState.value.dateRange?.[1],
  //     zjm: formState.value.zjm,
  //     operationmode: formState.value.operationmode
  //   }
  // },
  formatResult: (result) => {
    total.value = result.reduce(
      (sum, current) => {
        return {
          cbje: Number(sum.cbje || 0) + Number(current.cbje || 0),
          hjje: Number(sum.je || 0) + Number(current.je || 0)
        }
      },
      { xsje: 0 }
    )
    return result
  }
})
// const reset = () => {
//   formState.value = {
//     zjm: '',
//     dateRange: [dayjs(new Date().setDate(new Date().getDate() - 20)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
//   }
//   getData()
// }
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
